package org.jetlinks.pro.visualization.tenant;

import lombok.AllArgsConstructor;
import org.jetlinks.pro.assets.AssetSupplier;
import org.jetlinks.pro.assets.AssetType;
import org.jetlinks.pro.tenant.supports.DefaultAsset;
import org.jetlinks.pro.visualization.entity.VisualizationCatalogEntity;
import org.jetlinks.pro.visualization.service.VisualizationCatalogService;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author bestfeng
 */
@Component
@AllArgsConstructor
public class VisualizationCatalogAssetSupplier implements AssetSupplier {

    private final VisualizationCatalogService catalogService;


    @Override
    public List<AssetType> getTypes() {
        return Collections.singletonList(VisualizationAssetType.visualizationCatalog);
    }

    @Override
    public Flux<DefaultAsset> getAssets(AssetType type, Collection<?> assetId) {
        return catalogService
            .createQuery()
            .where()
            .in(VisualizationCatalogEntity::getId, assetId)
            .fetch()
            .map(ps -> new DefaultAsset(ps.getId(), ps.getName(), VisualizationAssetType.visualizationCatalog));
    }
}
